﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Painter3D.PhysicalSys;

namespace Painter3D
{
    public abstract class Component : IDisposable
    {
        /// <summary>
        /// 指示该组件是否启用
        /// </summary>
        public bool enable = true;
        /// <summary>
        /// 该组件吸附的节点
        /// </summary>
        protected Transform transform;
        public Transform Transform
        {
            set
            {
                transform = value ?? throw new ArgumentNullException(nameof(value));
                //进行组件环境检查
                if (!CompentRequired(out string info))
                {
                    throw new Exception("组件环境异常" + info);
                }
            }
            get
            {
                return transform;
            }
        }



        protected Component(Transform transform)
        {
            this.transform = transform;
            this.transform.AddComponent(this);
        }


        public void Update()
        {
            if (!enable)
            {
                return;
            }
            Loop();
        }

        #region 组件函数
        /// <summary>
        /// 检测该组件的前置组件是否提供
        /// </summary>
        /// <returns>true 表示组件环境正常</returns>
        virtual protected bool CompentRequired(out string info)
        {
            info = null;
            return true;
        }
        /// <summary>
        /// 组件初始化
        /// </summary>
       // virtual public void Init()
       // {
       // }
        /// <summary>
        /// 循环调用
        /// </summary>
        virtual protected void Loop()
        {

        }
        /// <summary>
        /// 组件移除的清理
        /// </summary>
        virtual public void Dispose()
        {

        }

        public override string ToString()
        {
            return this.GetType().Name + "->" + Environment.NewLine;
        }
        #endregion
    }
}
